<template>
  <!--
    新增或修改对话框：选择生产派工
    -->
  <el-dialog v-dialogDrag v-dialogDragWidth :title="dialog_selectTask.title" :visible.sync="dialog_selectTask.visible" width="1200px">

    <!-- 检索条件 -->
    <el-form :model="dialog_selectTask.queryParams" :inline="true" v-show="dialog_selectTask.showSearch" label-width="120px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="物料名称" prop="itemName">
            <el-input
              v-model="dialog_selectTask.queryParams.itemName"
              placeholder="请输入物料名称"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="计划开始时间">
            <el-date-picker
              v-model="dialog_selectTask.queryParams.dtPlanStart"

              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="计划完工时间" >
            <el-date-picker

              v-model="dialog_selectTask.queryParams.dtPlanDone"
              type=  "daterange"
              range-separator="-"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="实际开始时间" >
            <el-date-picker
              v-model="dialog_selectTask.queryParams.dtActualStart"
              type="daterange"
              style="width: 240px"
              range-separator="-"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="实际完工时间" >
            <el-date-picker
              v-model="dialog_selectTask.queryParams.dtActualDone"
              type="daterange"
              style="width: 240px"
              range-separator="-"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="计划班组" prop="planDeptName">
            <el-select
              filterable
              v-model="dialog_selectTask.queryParams.planDeptName"
              placeholder="请选择班组名称"
              clearable
              size="small"
              style="width: 240px">
              <el-option
                v-for="item in deptList"
                :key="item.deptName"
                :label="item.deptName"
                :value="item.deptName"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="计划负责人" prop="planNickName">
            <el-select
              filterable
              v-model="dialog_selectTask.queryParams.planNickName"
              placeholder="请选择计划负责人"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="item in masterList"
                :key="item.userId"
                :label="item.nickName"
                :value="item.nickName"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini"
                       @click="getList_dispatch">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>


    <!-- 生产派工列表 -->
    <el-table v-loading="loading" :data="dialog_selectTask.taskList" row-key="dispatchUuid">
      <el-table-column label="物料名称" align="center" prop="itemName" width="180px" fixed :show-overflow-tooltip="true"/>
      <el-table-column label="生产数量" align="center" prop="taskQty" width="80px"/>
      <el-table-column label="工序类型" align="center" prop="mesDictProcessType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_dict_process_type" :value="scope.row.mesDictProcessType"/>
        </template>
      </el-table-column>
      <el-table-column label="工序名称" align="center" prop="mesDictProcessName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_dict_process_name" :value="scope.row.mesDictProcessName"/>
        </template>
      </el-table-column>
      <el-table-column label="工序符号" align="center" prop="mesDictProcessCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_dict_process_code" :value="scope.row.mesDictProcessCode"/>
        </template>
      </el-table-column>
      <el-table-column label="完工标识" align="center" prop="sysYesNoDoneTask">
        <template slot-scope="scope">
          <dict-tag :type="scope.row.sysYesNoDoneTask == 'N' ? 'danger' : 'primary'"
                    :options="dict.type.sys_yes_no" :value="scope.row.sysYesNoDoneTask"/>
        </template>
      </el-table-column>
      <el-table-column label="计划数量" align="center" prop="planQty" width="150px">
        <template slot-scope="scope">
          <span> {{ Math.floor((scope.row.planQty)) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划班组" align="center" prop="planDeptName" width="150px"/>
      <el-table-column label="计划负责人" align="center" prop="planNickName" width="150px"/>
      <el-table-column label="计划开始时间" align="center" prop="planDtStart" width="150px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planDtStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划完工时间" align="center" prop="planDtDone" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planDtDone, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际数量" align="center" prop="doneQty" width="150px">
        <template slot-scope="scope">
          <span> {{ Math.floor((scope.row.doneQty)) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际班组" align="center" prop="actualDeptName" width="150px"/>
      <el-table-column label="实际负责人" align="center" prop="actualNickName" width="150px"/>
      <el-table-column label="实际开始时间" align="center" prop="dtStart" width="150px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dtStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际完工时间" align="center" prop="dtDone" width="150px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dtDone, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请选择" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button type="success" plain
                     size="small"
                     icon="el-icon-check"
                     @click="selectTask_done(scope.row)"
          ><span> 选择</span></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页查询-->
    <pagination
      v-show="dialog_selectTask.total>0"
      :total="dialog_selectTask.total"
      :page.sync="dialog_selectTask.queryParams.pageNum"
      :limit.sync="dialog_selectTask.queryParams.pageSize"
      @pagination="getList_dispatch"
    />

    <div slot="footer" class="dialog-footer">
      <el-button @click="dialog_hide_selectTask">取 消</el-button>
    </div>

  </el-dialog>

</template>

<script>
  import {listDispatch} from "@/api/mes/dispatch";
  import { listDept } from "@/api/system/dept";
  import { listUser} from "@/api/system/user";
  export default {
    dicts: [ 'sys_yes_no','mes_dict_process_type','mes_dict_process_name','mes_dict_process_code','sys_normal_disable'],
    data() {
      return {
        dialog_selectTask: {
          visible: false,
          showSearch: true,
          title: "请选择物料",
          total: 0,
          taskList: [],
          deptList:[],
          masterList:[],
          queryParams:{
            itemName: null,
          }
        },
        //部门下拉
        deptList:[],
        //负责人
        masterList:[],
        // 遮罩层
        loading: true,
        // 遮罩层
        visible: false,
        params_dept:{},
        // 总条数
        total: 0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          roleId: undefined,
          userName: undefined,
          phonenumber: undefined
        },
      };
    },
    created(){
      this.getList_dept()
      this.getList_master()
    },
    methods: {
      /** 列表: 部门（班组） */
      getList_dept(){
        /**班组数据下拉列表查询*/
        this.getConfigKey("workers_group_id").then(response => {
          console.log(response)
          this.params_dept.parentId = response.msg
          listDept(this.params_dept).then(response => {
            console.log(response)
            this.deptList = response.data
          });
        });
      },

      /** 列表: 负责人 */
      getList_master(){
        listUser().then(response => {
          this.masterList = response.rows
        });
      },

      /** 读取列表：生产派工 */
      getList_dispatch() {
        this.loading = true;
        if( this.dialog_selectTask.queryParams.dtPlanStart ){
          this.dialog_selectTask.queryParams.dtPlanStartBegin = this.dialog_selectTask.queryParams.dtPlanStart[0]
          this.dialog_selectTask.queryParams.dtPlanStartEnd = this.dialog_selectTask.queryParams.dtPlanStart[1]
        }else {
          this.dialog_selectTask.queryParams.dtPlanStartBegin = null
          this.dialog_selectTask.queryParams.dtPlanStartEnd = null
        }
        if( this.dialog_selectTask.queryParams.dtPlanDone ){
          this.dialog_selectTask.queryParams.dtPlanDoneBegin = this.dialog_selectTask.queryParams.dtPlanDone[0]
          this.dialog_selectTask.queryParams.dtPlanDoneEnd = this.dialog_selectTask.queryParams.dtPlanDone[1]
        }else {
          this.dialog_selectTask.queryParams.dtPlanDoneBegin = null
          this.dialog_selectTask.queryParams.dtPlanDoneEnd = null
        }
        if( this.dialog_selectTask.queryParams.dtActualStart ){
          this.dialog_selectTask.queryParams.dtActualStartBegin = this.dialog_selectTask.queryParams.dtActualStart[0]
          this.dialog_selectTask.queryParams.dtActualStartEnd = this.dialog_selectTask.queryParams.dtActualStart[1]
        }else {
          this.dialog_selectTask.queryParams.dtActualStartBegin = null
          this.dialog_selectTask.queryParams.dtActualStartEnd = null
        }
        if( this.dialog_selectTask.queryParams.dtActualDone ){
          this.dialog_selectTask.queryParams.dtActualDoneBegin = this.dialog_selectTask.queryParams.dtActualDone[0]
          this.dialog_selectTask.queryParams.dtActualDoneEnd = this.dialog_selectTask.queryParams.dtActualDone[1]
        }else {
          this.dialog_selectTask.queryParams.dtActualDoneBegin = null
          this.dialog_selectTask.queryParams.dtActualDoneEnd = null
        }

        /** 读取列表：生产派工 */
        listDispatch(this.dialog_selectTask.queryParams).then(response => {
          console.log( response )
          this.dialog_selectTask.taskList = response.rows;
          this.dialog_selectTask.total = response.total;
          this.loading = false;
        });
      },

      /**完成：选择物料*/
      selectTask_done(task) {
        this.dialog_selectTask.visible = false;
        this.$emit("ok", task );
        console.log( task )
      },

      /**对话框：显示 - 选择生产派工*/
      dialog_show_selectTask() {
        this.getList_dispatch()
        this.dialog_selectTask.visible = true;
        this.dialog_selectTask.title = "选择生产派工";
      },

      /**对话框：隐藏 - 选择生产派工*/
      dialog_hide_selectTask() {
        this.dialog_selectTask.visible = false;
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },

      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
    }
  };
</script>
